package yxy.game.pm2.command.jadeseal;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.medalrefine;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.JadeSealMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.jadeseal.JadeSeal;
import yxy.game.pm2.module.jadeseal.JadeSealModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

/** 玉玺 阵营玉玺升阶 */
@MessageType(proto = Command.JadeSealStageUpReset, classtype = JadeSealMessage.JadeSealStageUpResetRequest.class)
public class CMD_JadeSealStageUpReset extends Command<JadeSealMessage.JadeSealStageUpResetRequest> {

	@Override
	public void execute(MessageSource messageSource, JadeSealMessage.JadeSealStageUpResetRequest request) {
		String playerCode = messageSource.getTag();
		int camp = request.getCamp();
		JadeSealMessage.JadeSealStageUpResetResponse.Builder response = JadeSealMessage.JadeSealStageUpResetResponse.newBuilder();
		response.setCamp(camp);
		
		JadeSealModule jsmodule = JadeSealModule.get(playerCode);
		if(jsmodule == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		JadeSeal js = jsmodule.getJss().get(camp);
		if(js == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(!js.isOpen()) {
			send(messageSource, response, Result.JadeSealNeedUnlock);
			return;
		}
				
		if(js.getStagelevel() == 0 && js.getStagestage() == 0) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		int mrid = js.countmrid();
		
		ArrayList<Integer> ids = new ArrayList<Integer>();
		Map<String, medalrefine> mrmap = DataCenter.getAllData(medalrefine.class);
		for(medalrefine mr : mrmap.values()) {
			if(mr.getType() != camp) {
				continue;
			}
			
			if(mr.getId() >= mrid) {
				continue;
			}
			ids.add(mr.getId());
		}
		Collections.sort(ids);

		ArrayList<Currency> curs = new ArrayList<Currency>();
		for(int id : ids) {
			medalrefine mr = mrmap.get(String.valueOf(id));
			Collection<Currency> idcurs = Currency.create(mr.getCosttype(), mr.getCostid(), mr.getCostnum());
			curs.addAll(idcurs);
		}
		
		Collection<Currency> mergecur = Currency.merge(curs);
		
		js.setStagelevel(0);
		js.setStagestage(0);
		jsmodule.set();

		PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
		PlayTypeHandle.saveJadeSeal(playerCode,playTypeModule);//提前把战力算到中间缓存

		new BattleHandle().powerUpdate(playerCode);// 战力更新

		new CurrencyHandle().addCurrency(playerCode, CCReason.玉玺, mergecur);

		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(mergecur);
		response.addAllDisplay(items);
		response.setStagelevel(js.getStagelevel());
		response.setStagestage(js.getStagestage());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, JadeSealMessage.JadeSealStageUpResetResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}